package org.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import org.ServerThread;
import org.util.Database;

public class ChessDAO {
	public boolean insertData(int clientId,String pStr,String dateStr,int myType,String cname,int ddr,int number){
		Connection conn=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		Database db=new Database();
		String sql="INSERT INTO chess(id,coord,playtime,step,clientname,dr,stepNumber) VALUES (?,?,?,?,?,?,?)";
		conn=db.getConn();
		int a=0;
		try {
			ps=conn.prepareStatement(sql);
			ps.setInt(1, clientId);
			ps.setString(2, pStr);
			ps.setString(3, dateStr);
			ps.setInt(4, myType);
			ps.setString(5, cname);
			ps.setInt(6, ddr);
			ps.setInt(7, number);
			a=ps.executeUpdate();
			db.closConn(conn, ps);	
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if(a>0){
			return true;
		}else{
			return false;
		}
		
	}
	public boolean deleteData(int Number,int type){
		Connection conn=null;
		PreparedStatement ps=null;
		String sql="UPDATE chess SET dr=1 WHERE stepNumber>=? and step=?";
		Database db=new Database();
		conn=db.getConn();
		int a=0;
		try{
			ps=conn.prepareStatement(sql);
			ps.setInt(1, Number);
			ps.setInt(2, type);
			a=ps.executeUpdate();
			db.closConn(conn, ps);
		}catch(Exception e){
			System.out.println(e);
		}
		if(a>0){
			return true;
		}else{
			return false;
		}
		
	}
	public boolean insertData1(int id,String stime,String etime,String winer,int dr){
		Connection conn=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		Database db=new Database();
		String sql="INSERT INTO game(id,starttime,endtime,winer,dr) VALUES(?,?,?,?,?)";
		conn=db.getConn();
		int a=0;
		try{
			ps=conn.prepareStatement(sql);
			ps.setInt(1, id);
			ps.setString(2, stime);
			ps.setString(3, etime);
			ps.setString(4, winer);
			ps.setInt(5, dr);
			a=ps.executeUpdate();
			db.closConn(conn, ps);
		}catch(Exception e){
			System.out.println(e);
		}
		if(a>0){
			return true;
		}else{
			return false;
		}
	}
}
